package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.constants.MarketConstants;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.exception.ServiceStringException;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.admin.category.req.CategoryClassAddReq;
import com.hxzy.controller.admin.category.req.CategoryClassEditReq;
import com.hxzy.controller.admin.category.req.CategoryClassSearchReq;
import com.hxzy.controller.admin.category.resp.CategoryClassResp;
import com.hxzy.controller.mobile.category.resp.MobileCategoryClassResp;
import com.hxzy.entity.CategoryClass;
import com.hxzy.mapstruct.CategoryClassMapStruct;
import com.hxzy.service.CategoryClassService;
import com.hxzy.mapper.CategoryClassMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author admin
* @description 针对表【category_class(商品分类表)】的数据库操作Service实现
* @createDate 2024-02-21 11:30:40
*/
@Service
public class CategoryClassServiceImpl extends ServiceImpl<CategoryClassMapper, CategoryClass>
    implements CategoryClassService{

    @Override
    public List<CategoryClassResp> search(CategoryClassSearchReq req) {
        return this.baseMapper.search(req);
    }

    @Override
    public boolean addData(CategoryClassAddReq req) {
        //判断名称是否被使用
        LambdaQueryWrapper<CategoryClass> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryClass::getName,req.getName());
        queryWrapper.eq(CategoryClass::getMerchantId,MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getId());
        if(this.count(queryWrapper)>0){
            throw new ServiceStringException("商品分类已经被使用了");
        }

        CategoryClass entity = CategoryClassMapStruct.INSTANCE.convert(req);
        entity.setStatus(MarketConstants.ACCOUT_ENABLE);
        entity.setCreateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());
        entity.setCreateTime(new Date());
        entity.setMerchantId(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getId());
        return this.save(entity);
    }

    /**
     * 修改商品分类
     * @param req
     * @return
     */
    @Override
    public boolean editData(CategoryClassEditReq req) {
        CategoryClass db = this.getById(req.getId());
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.NOT_FOUND_DATA);
        }

        //判断名称是否被使用
        LambdaQueryWrapper<CategoryClass> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryClass::getName,req.getName());
        queryWrapper.eq(CategoryClass::getMerchantId,MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getId());
        queryWrapper.ne(CategoryClass::getId,req.getId());

        if(this.count(queryWrapper)>0){
            throw new ServiceStringException("商品分类已经被使用了");
        }

        //赋值
        db.setName(req.getName());
        db.setPicture(req.getPicture());
        db.setUpdateBy(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get().getName());
        db.setCreateTime(new Date());
        return this.updateById(db);
    }

    /**
     * 商家分类列表
     * @param merchantId
     * @return
     */
    @Override
    public List<MobileCategoryClassResp> mobileList(Long merchantId) {
        return this.baseMapper.mobileList(merchantId);
    }


}




